package com.santiao.publics.util.oaid;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;


import com.santiao.publics.config.Const;
import com.santiao.publics.util.SharedPreferencesUtil;
import com.wifiKey.android.util.oaid.StringIds;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.NetworkInterface;

public class StringId {

    public  static  void  idCache(Context context){
        macAddressCache(context);
        androidIdndressCache(context);
        imeindressCache(context);
    }
    public static void macAddressCache(Context context){
        SharedPreferencesUtil.saveString("mac", new StringIds().getMacFromHardware(context));
    }
    public static void   androidIdndressCache(Context context){
        SharedPreferencesUtil.saveString("androidId", new StringIds().getAndroidId(context));
    }
    public static  void imeindressCache(Context context){
        SharedPreferencesUtil.saveString("imei", new StringIds().getImei(context));
    }


    public static final String NULL = null;

    public static String getMacAddress() {
        /*获取mac地址有一点需要注意的就是android 6.0版本后，以下注释方法不再适用，不管任何手机都会返回"02:00:00:00:00:00"这个默认的mac地址，这是googel官方为了加强权限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法来获得mac地址。*/
        //        String macAddress= "";
//        WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
//        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//        macAddress = wifiInfo.getMacAddress();
//        return macAddress;
        try {
            String macAddress;
            StringBuffer buf = new StringBuffer();
            NetworkInterface networkInterface;
            networkInterface = NetworkInterface.getByName("eth1");
            if (networkInterface == null) {
                networkInterface = NetworkInterface.getByName("wlan0");
            }
            if (networkInterface == null) {
                return null;
            }
            byte[] addr = networkInterface.getHardwareAddress();
            for (byte b : addr) {
                buf.append(String.format("%02X:", b));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            macAddress = buf.toString();
            return macAddress;
        } catch (Throwable e) {
            return null;
        }
    }

    public static String getAndroidId(Context context) {
        try {
//                  return Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
            String id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            return  id ;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getIMEIs(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10+ 不允许获取 IMEI、MEID 之类的设备唯一标识
            Log.d(Const.TAG,"IMEI/MEID not allowed on Android 10+");
            return null;
        }
        if (context == null) {
            return null;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) !=
                PackageManager.PERMISSION_GRANTED) {
            Log.d(Const.TAG,"android.permission.READ_PHONE_STATE not granted");
            // Android 6.0-9.0 需要申请电话权限才能获取设备唯一标识
            return NULL;
        }
        return getIMEI(context);
    }

    private static String getIMEI(Context context) {
        if (context == null) {
            return null;
        }
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getImei();
            if (TextUtils.isEmpty(imei)) {
                imei = tm.getMeid();
            }
            return imei;
        } catch (Exception e) {
            return null;
        }

    }

    public static String readAssetFile(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream = assetManager.open(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append('\n');
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
